package com.yuyou.fn.tas.pusher.handler;

import com.alibaba.fastjson.JSONArray;
import com.google.common.collect.ImmutableListMultimap;
import com.google.common.collect.Multimaps;
import com.yuyou.fn.common.database.event.EventHandler;
import com.yuyou.fn.common.database.event.IEventHandler;
import com.yuyou.fn.educational.entity.Campus;
import com.yuyou.fn.tas.pusher.service.IDataPushService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

@EventHandler(tables = "yyfn.campus")
@Component
public class CampusPusher  implements IEventHandler {

    @Resource
    private IDataPushService dataPushService;

    @Override
    public void insert(String tableName, JSONArray rowList) {
        handle(rowList);
    }



    @Override
    public void update(String tableName, JSONArray rowList, Set<String> updateColumns) {
        handle(rowList);
    }

    @Override
    public void delete(String tableName, JSONArray rowList) {

    }


    private void handle(JSONArray rowList) {
        List<Campus> campuses = rowList.toJavaList(Campus.class);

        ImmutableListMultimap<Long, Campus> areaTeamId2Campus = Multimaps.index(campuses, c -> c.getAreaTeamId());

        for(Long areaTeamId : areaTeamId2Campus.keySet()){
            dataPushService.pushCampus(areaTeamId,areaTeamId2Campus.get(areaTeamId));
        }
    }
}
